import {
  defaultPostAsync,
  getByObjectArgsAsync,
  getByUrlAsync,
  deleteAsync,
  deleteByUrlAsync,
  patchByUrlAsync
} from '@/utils/http-client';

import { AuthMode } from '@/types/HttpClient';
// import type { DefaultHttpActionResult } from '@/types/HttpClient';
import type { ProjectInfo } from '@/types/ProjectInfo';
// import { Part } from '@/types/system/device-maintenance/part';
// import { PollingTask } from '@/types/system/device-maintenance/polling-task';
// import { ComeGroup } from '@/types/system/device-maintenance/come-group';

import { Part } from '@/types/Part';
import { PollingTask } from '@/types/PollingTask';

const baseUrl = import.meta.env.VITE_DEVICE_MAINTENANCE_BASE_URL;

export async function insertProjectAsync(project: ProjectInfo) {
  return defaultPostAsync(baseUrl + '/api/device-maintenance/projects', project, AuthMode.Bearer);
}

export async function getProjectsAsync(skipCount: number, maxResultCount: number) {
  return getByObjectArgsAsync<ProjectInfo[]>(
    baseUrl + '/api/device-maintenance/projects',
    { skipCount: skipCount, maxResultCount: maxResultCount },
    AuthMode.Bearer
  );
}

export async function deleteProjectAsync(id: string) {
  return deleteAsync(baseUrl + '/api/device-maintenance/projects', id, AuthMode.Bearer);
}

export async function getPartsAsync(projectId: string) {
  return getByUrlAsync<Part[]>(
    baseUrl + `/api/device-maintenance/parts/by-project-id/${projectId}`,
    AuthMode.Bearer
  );
}

export async function insertPartAsync(newPartInfo: Part) {
  return defaultPostAsync(baseUrl + '/api/device-maintenance/parts', newPartInfo, AuthMode.Bearer);
}

export async function deletePartAsync(id: string) {
  return deleteByUrlAsync(baseUrl + '/api/device-maintenance/parts/' + id, AuthMode.Bearer);
}

export async function getPollingTasksAsync(partId: string) {
  return getByUrlAsync<PollingTask[]>(
    baseUrl + '/api/device-maintenance/polling-tasks/by-part-id/' + partId,
    AuthMode.Bearer
  );
}

export async function insertPollingTaskAsync(task: PollingTask) {
  return await defaultPostAsync(
    baseUrl + '/api/device-maintenance/polling-tasks',
    task,
    AuthMode.Bearer
  );
}

export async function deletePollingTaskAsync(id: string) {
  return await deleteAsync(baseUrl + '/api/device-maintenance/polling-tasks', id, AuthMode.Bearer);
}

export async function setPartPollingTaskEnablePropertyAsync(id: string, enable: boolean) {
  if (enable) {
    return await patchByUrlAsync(
      baseUrl + '/api/device-maintenance/part-polling-tasks/' + id + '/enable',
      AuthMode.Bearer
    );
  } else {
    return await patchByUrlAsync(
      baseUrl + '/api/device-maintenance/part-polling-tasks/' + id + '/disable',
      AuthMode.Bearer
    );
  }
}

// export async function insertComeGroupAsync(comeGroup: ComeGroup) {
//   return await defaultPostAsync(
//     baseUrl + '/api/device-maintenance/come-groups',
//     comeGroup,
//     AuthMode.Bearer
//   );
// }

export async function deleteComeGroupAsync(id: string) {
  return await deleteByUrlAsync(
    baseUrl + '/api/device-maintenance/come-groups/' + id,
    AuthMode.Bearer
  );
}

// export async function getComeGroupAsync(projectId: string) {
//   return await getByUrlAsync<ComeGroup | null>(
//     baseUrl + '/api/device-maintenance/come-groups/by-project-id/' + projectId,
//     AuthMode.Bearer
//   );
// }
